local skel = fk.CreateSkill {
  name = "lb_zero__fengxue",
}

Fk:loadTranslationTable {
  ["lb_zero__fengxue"] = "锋谑",
  [":lb_zero__fengxue"] = "准备阶段，你可令一名角色猜测你本回合是否会对其造成伤害，且其本回合不可求救。回合结束时，若其猜错，你获得其一张牌，且其本轮不可求救。",

  ["@[player]lb_zero__fengxue-round"] = "锋谑",
  ["@lb_zero__fengxue-turn"] = "锋谑",

  ["#lb_zero__fengxue"] = "锋谑：你可以令一名角色猜测你本回合是否会对其造成伤害，且其本回合不可求救",
  ["#lb_zero__fengxue-ask"] = "锋谑：请猜测本回合 %src 是否会对你造成伤害",
  ["#lb_zero__fengxue-choose"] = "锋谑：获得 %src 一张牌",

  ["#lb_zero__fengxue-log"] = "锋谑：%from 选择了 %arg",

  ["$lb_zero__fengxue1"] = "不痛不痛~",
  ["$lb_zero__fengxue2"] = "不准逃~",
}

skel:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_cost = function(self, event, target, player, data)
    local tos = player.room:askToChoosePlayers(player, {
      targets = player.room.alive_players,
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      cancelable = true,
      prompt = "#lb_zero__fengxue",
    })
    if #tos > 0 then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local choice = room:askToChoice(to, {
      choices = { "yes", "no" },
      skill_name = skel.name,
      prompt = "#lb_zero__fengxue-ask:" .. player.id,
    })
    room:setPlayerMark(player, "lb_zero__fengxue-turn", {to, choice})
    room:setPlayerMark(player, "@lb_zero__fengxue-turn", to)
    room:sendLog{
      type = "#lb_zero__fengxue-log",
      from = to.id,
      arg = choice,
      toast = true,
    }
  end,
})

skel:addEffect(fk.TurnEnd, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local list = player:getTableMark("lb_zero__fengxue-turn")
    if player:hasSkill(skel.name) and target == player and #list == 2 and not list[1].dead then
      local isDamage = #player.room.logic:getEventsOfScope(GameEvent.Damage, 1, function(e)
        local damage = e.data
        return damage.from == player and damage.to == list[1] and not damage.prevented
      end, Player.HistoryTurn) == 1
      if list[2] == "yes" then
        return not isDamage
      else
        return isDamage
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local list = player:getTableMark("lb_zero__fengxue-turn")
    local to = list[1]
    room:addTableMarkIfNeed(player, "@[player]lb_zero__fengxue-round", to.id)
    if #to:getCardIds("he") > 0 then
      local card = room:askToChooseCard(player, {
        target = to,
        flag = "he",
        skill_name = skel.name,
        prompt = "#lb_zero__fengxue-choose:" .. to.id,
      })
      room:obtainCard(player, card, false, fk.ReasonPrey, player, skel.name)
    end
  end,
})

skel:addEffect(fk.AskForPeaches, {
  can_refresh = function (self, event, target, player, data)
    local list = player:getTableMark("lb_zero__fengxue-turn")
    return player:hasSkill(skel.name) and 
    (
      (#list == 2 and not list[1].dead and list[1] == data.who) or 
      table.contains(player:getTableMark("@[player]lb_zero__fengxue-round"), data.who.id)
    )
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setTag("SkipGameRule", true)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
    local room = player.room
    room:setPlayerMark(player, "lb_zero__fengxue-turn", 0)
    room:setPlayerMark(player, "@lb_zero__fengxue-turn", 0)
    room:setPlayerMark(player, "@[player]lb_zero__fengxue-round", 0)
end)

return skel
